﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Configuration;
using System.IO;
using System.Xml.Linq;
using System.Collections.Specialized;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;

namespace AutoCadConvert
{
    public class House
    {
        #region xml节点数据
        public int Version = 100;
        public string Type = "CustomHome";
        //xml结构
        public List<BspObject> objList = new List<BspObject>();
        #endregion

        public ModelFactory factory = new ModelFactory();
        public Dictionary<string, string> Dict { get { return factory.Dict; }}

        //查找数据
        public T Item<T>() where T : BspObject
        {
            if (factory.IndexOf(typeof(T)) != -1)
                return objList[factory.IndexOf(typeof(T))] as T;
            else
                return null;
        }

        public House()
        {
            foreach (string obj in factory.Setting)
            {
                Type type = factory.GetBspObjectType(obj);
                if (type != null) objList.Add(factory.CreateBspObject(type, this));
            }
        }
        public void AddData(Entity value)
        {
            string sValue = factory.Dict[value.Layer.Trim()];
            //只解析配置中包含的对象
            foreach (string it in sValue.Split(new char[] { ',' }))
            {
                Type type = factory.GetBspObjectType(it);
                if (type == null)
                {
                    DwgConvert.PutMessage("配置文件中找不到节点" + it);
                    continue;
                }
                if (factory.IndexOf(type) >= 0)
                    objList[factory.IndexOf(type)].AddData(value);
                else
                    factory.CreateBspObject(type, this).AddData(value);
            }
        }
        public void AjustLineAndPoint()
        {
            //调整数据
            foreach (BspObject item in objList)
                item.AjustLineAndPoint();

            //处理不导出数据
            foreach (BspObject item in factory.cacheData)
                item.AjustLineAndPoint();
        }

        #region 序列化
        public IEnumerable<XElement> XmlChild()
        {
            int idx = 0;
            foreach(BspObject item in objList)
            {
                if(item.GetType()==typeof(DMSVersion))
                    yield return item.ToXml().FirstOrDefault();
                else
                    yield return new XElement(factory.Setting[idx],item.ToXml());
                idx++;
            }
        }
        public XDocument XmlSerialize()
        {
            //导出xml
            return new XDocument(
                new XElement("Home", new XAttribute("Version", Version), new XAttribute("CustomHome", Type),
                    XmlChild().ToArray<XElement>()
                    )
                );
        }
        #endregion

        #region 反序列化
        public IEnumerable<BspObject> XmlParse(XDocument doc)
        {
            XElement root = doc.Element("Home");
            foreach (XElement element in root.Elements())
            {
                foreach (string item in factory.Setting)
                    if (element.Name == item)
                        return factory.CreateBspObject(factory.GetBspObjectType(item)).FromXml(element.Element(item));
            }

            return new BspObject[]{ };
        }
        public House XmlDeserialize(string data)
        {
            //导出xml
            XDocument doc = XDocument.Load(data);
            return new House() { objList = XmlParse(doc).ToList(),factory=factory};
        }
        #endregion
    }
}
